package com.example.mynote;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NoteAdapter extends ArrayAdapter<Note> {
    private int resourceId;
    private Context context;

    private List<Note> noteList;
    private List<Note> backList; // 备份原始数据
    private MyFilter mFilter;

    private HashMap<Integer, Integer> isCheckBoxVisibleMap;// 用来记录是否显示checkBox
    private HashMap<Integer, Boolean> isCheckedMap;// 用来记录是否被选中
    private boolean isMultiSelect = false;// 是否处于多选状态


    public NoteAdapter(@NonNull Context context, int resource, @NonNull List<Note> noteList) {
        super(context, resource, noteList);
        resourceId = resource;
        this.context = context;
        this.noteList = noteList;
        backList = noteList;

        isCheckBoxVisibleMap = new HashMap<Integer, Integer>();
        isCheckedMap = new HashMap<Integer, Boolean>();
        // 初始化状态
        isMultiSelect(false);
        isCheckedALL(false);

    }

    public void setMultiSelect(boolean multiSelect) {
        isMultiSelect = multiSelect;
    }

    public boolean isMultiSelect() {
        return isMultiSelect;
    }

    public HashMap<Integer, Boolean> getIsCheckedMap() {
        return isCheckedMap;
    }

    public void setIsCheckedMap(HashMap<Integer, Boolean> isCheckedMap) {
        this.isCheckedMap = isCheckedMap;
    }

    // 是否多选状态
    public void isMultiSelect(boolean f) {
        // 设置状态
        isMultiSelect = f;
        if (isMultiSelect) {
            for (int i = 0; i < noteList.size(); i++) {
                isCheckBoxVisibleMap.put(i, CheckBox.VISIBLE);
            }
        } else {
            for (int i = 0; i < noteList.size(); i++) {
                isCheckBoxVisibleMap.put(i, CheckBox.INVISIBLE);
            }
        }
    }

    // 是否全部选中
    public void isCheckedALL(boolean f) {
        for (int i = 0; i < noteList.size(); i++) {
            isCheckedMap.put(i, f);
        }
    }

    //内部类：用于对控件的实例进行缓存，没有必要每次都通过findViewById() 方法来获取控件实例
    class ViewHolder {
        TextView tv_content;
        TextView tv_time;
        CheckBox cb;
    }


    // 重写getView方法
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Note note = getItem(position);
        LayoutInflater lf = LayoutInflater.from(context);
        View view;
        ViewHolder vh;
        if (convertView != null) {
            view = convertView;
            //直接获取控件
            vh = (ViewHolder) view.getTag();
        } else {
            // 加载到parent布局中 false表示不为这个View添加父布局
            view = lf.inflate(resourceId, parent, false);
            vh = new ViewHolder();
            //找到两控件
            vh.tv_content = (TextView) view.findViewById(R.id.tv_content);
            vh.tv_time = (TextView) view.findViewById(R.id.tv_time);
            vh.cb = (CheckBox) view.findViewById(R.id.note_check_bnt);
            view.setTag(vh);
        }
        // 获取自定义布局的控件
        // 设置数据
        vh.tv_content.setText(note.getContent());
        vh.tv_time.setText(note.getTime());
        // 根据position设置CheckBox是否可见，是否选中
        if (!isMultiSelect) { //不是选择框的时候
            // 重置控件状态
            isMultiSelect(false);
            isCheckedALL(false);
        }
        vh.cb.setChecked(isCheckedMap.get(position));
        vh.cb.setVisibility(isCheckBoxVisibleMap.get(position));
        return view;
    }

    /*这些都需要重写 因为过滤器会改变适配器里面的noteList数据，而外部绑定的noteList没有变*/
    @Override
    public int getCount() {
        return noteList.size();
    }

    @Override
    public Note getItem(int position) {
        return noteList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new MyFilter();
        }
        return mFilter;
    }



    class MyFilter extends Filter {
        //我们在performFiltering(CharSequence charSequence)这个方法中定义过滤规则
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults result = new FilterResults();
            List<Note> list;
            if (TextUtils.isEmpty(charSequence)) {//当过滤的关键字为空的时候，我们则显示所有的数据
                list = backList;
            } else {//否则把符合条件的数据对象添加到集合中
                list = new ArrayList<>();
                for (Note note : backList) {
                    if (note.getContent().contains(charSequence)) { // 包含关键字的笔记筛选出来
                        list.add(note);
                    }

                }
            }
            result.values = list; //将得到的集合保存到FilterResults的value变量中
            result.count = list.size();//将集合的大小保存到FilterResults的count变量中
            return result;
        }
        //在publishResults方法中告诉适配器更新界面
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            noteList = (List<Note>)results.values;
            if (results.count>0){
                notifyDataSetChanged();//通知数据发生了改变
            }else {
                notifyDataSetInvalidated();//通知数据失效
            }
        }
    }
}
